<%@ Page language="vb" CodeFile="editClient.aspx.vb" Inherits="editClient_aspx_vb" %>

<%
    rowNumber = 0
    If (Request.QueryString("do")) = Nothing Then 
        subDo = Request.QueryString("subDo")
        intClientID = Request.QueryString("clientID")
        vehicleID = Request.QueryString("vehicleID")
        If Len(intClientID) > 0 Then
            intClientID = CLng(intClientID)
        ElseIf Len(vehicleID) > 0 Then
            vehicleID = CLng(vehicleID)
            dbOpen()
            RecSet = Conn.Execute("Select clientID From tblVehicle Where vehicleID = " & vehicleID & "")
            intClientID = RecSet.Fields("clientID").Value
            RecSet.Close()
            RecSet = Nothing
            dbClose()
        Else
            Response.Redirect("default.aspx")
        End If
        If Len(vehicleID) > 0 Then 
            vehicleID = CLng(vehicleID)
        End IF
        dbOpen()
        RecSet = Conn.Execute("Select c.client, c.clientType, c.cin, c.street, c.zipcode, c.city, c.areacode, c.telephone, c.email, co.country From (tblClient c INNER JOIN tblCountry co ON co.countryID = c.countryID) Where c.clientID = " & intClientID & "")
        If RecSet.EOF Then 
            Response.Redirect("searchClient.aspx")
        Else
            client = RecSet.Fields("client").Value
            clientType = RecSet.Fields("clientType").Value
            cin = RecSet.Fields("cin").Value
            street = RecSet.Fields("street").Value
            zipcode = RecSet.Fields("zipcode").Value
            city = RecSet.Fields("city").Value
            areacode = RecSet.Fields("areacode").Value
            telephone = RecSet.Fields("telephone").Value
            email = RecSet.Fields("email").Value
            country = RecSet.Fields("country").Value
        End IF
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        dbOpen()
        RecSet = Conn.Execute("Select c.countryCurrency, (Select Count(v.vehicleID) From tblVehicle v Where v.clientID = cl.clientID) As vehicleCount, (Select Count(d.driverID) From tblDriver d Where d.clientID = cl.clientID) As driverCount, (Select Count(c.claimID) From (tblClaim c INNER JOIN tblVehicle v ON v.vehicleID = c.vehicleID) Where v.clientID = cl.clientID) As claimCount, (Select Sum(c.paymentAmount) From (tblClaim c INNER JOIN tblVehicle v ON v.vehicleID = c.vehicleID) Where v.clientID = cl.clientID) As claimSum From (tblClient cl INNER JOIN tblCountry c ON c.countryID = cl.countryID) Where cl.clientID = " & intClientID & "")
        If Not RecSet.EOF Then 
            countryCurrency = RecSet.Fields("countryCurrency").Value
            vehicleCount = RecSet.Fields("vehicleCount").Value
            driverCount = RecSet.Fields("driverCount").Value
            claimCount = RecSet.Fields("claimCount").Value
            claimSum = RecSet.Fields("claimSum").Value
        End IF
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        If IsDBNull(claimSum) Then 
            claimSum = 0
        End IF
        If CDbl(vehicleCount) = 0 And CDbl(driverCount) = 0 Then 
%>
<script type="text/javascript">
  function deleteCheck() { 
	if (window.confirm('OBS! Are you sure you want to delete this lease taker?')) {
	    location.href = 'editClient.aspx?clientID=<%= intClientID %>&do=delete';
	}
	else {
		return false;
	}
  }
  </script>
<%
        End IF
        Over()
        strJS = ""
        strJS = strJS + "[""clientType"", ""<1""],"
        strJS = strJS + "[""client"", ""==''""],"
        strJS = strJS + "[""cin"", ""==''""]"
        checkFields()
        Response.Write("<table cellpadding='0' cellspacing='0' border='0' width='1000'>")
        Response.Write("<tr>")
        Response.Write("<td valign='top'>")
        Response.Write("<table cellpadding='2' cellspacing='2' width='450'>")
        Response.Write("<tr><td class='headline' colspan='3'>EDIT LEASE TAKER<font color='#fcbc00'>" & subDo & "</font><hr class='hrHeadline'></td></tr>")
Response.Write("<form action='editClient.aspx?do=save&clientID=" & intClientID & "' method='post' onsubmit=""javascript:return checkFields();"">")
        Response.Write("<tr><td class='formBold'>Country:</td><td align='right'><input type='text' name='country' id='country' value='" & country & "' size='40' maxlength='50' readonly='readonly'></td></tr>")
        Response.Write("<tr>")
        Response.Write("<td class='formBold'>Lease taker type:</td>")
        Response.Write("<td align='right'>")
        Response.Write("<select name='clientType' id='clientType'>")
        Response.Write("<option value=''> Select option  ")
        Response.Write("<option value='1'")
        If CInt(clientType) = 1 Then 
            Response.Write(" selected='selected'")
        End IF
        Response.Write("> Private  ")
        Response.Write("<option value='2'")
        If CInt(clientType) = 2 Then 
            Response.Write(" selected='selected'")
        End IF
        Response.Write("> Company  ")
        Response.Write("<option value=''> -------------------------------------- ")
        Response.Write("</select>")
        Response.Write("</td>")
        Response.Write("<td><div id='error_clientType' class='edit_error'>&laquo; OBS!</div></td>")
        Response.Write("</tr>")
        Response.Write("<tr><td class='formBold'>Lease taker:</td><td align='right'><input type='text' name='client' id='client' value='" & client & "' size='40' maxlength='50'></td><td><div id='error_client' class='edit_error'>&laquo; OBS!</div></td></tr>")
        Response.Write("<tr><td class='formBold'>Corporate identity number:</td><td align='right'><input type='text' name='cin' id='cin' value='" & cin & "' size='40' maxlength='50'></td><td><div id='error_cin' class='edit_error'>&laquo; OBS!</div></td></tr>")
        Response.Write("<tr><td class='formBold'>Street:</td><td align='right'><input type='text' name='street' id='street' value='" & street & "' size='40' maxlength='50'></td></tr>")
        Response.Write("<tr><td class='formBold'>Zipcode:</td><td align='right'><input type='text' name='zipcode' id='zipcode' value='" & zipcode & "' size='40' maxlength='50' onkeyup='re=/(\d*)/; re.exec(this.value); this.value=RegExp.$1;'></td></tr>")
        Response.Write("<tr><td class='formBold'>City:</td><td align='right'><input type='text' name='city' id='city' value='" & city & "' size='40' maxlength='50'></td></tr>")
        Response.Write("<tr><td class='formBold'>Areacode:</td><td align='right'><input type='text' name='areacode' id='areacode' value='" & areacode & "' size='40' maxlength='50'></td></tr>")
        Response.Write("<tr><td class='formBold'>Telephone:</td><td align='right'><input type='text' name='telephone' id='telephone' value='" & telephone & "' size='40' maxlength='50'></td></tr>")
        Response.Write("<tr><td class='formBold'>Email:</td><td align='right'><input type='text' name='email' id='email' value='" & email & "' size='40' maxlength='50'></td></tr>")
        Response.Write("<tr>")
        Response.Write("<td align='right' colspan='2'>")
        If CDbl(vehicleCount) = 0 And CDbl(driverCount) = 0 Then 
            Response.Write("<input type='button' value='Delete lease taker' onclick=""{return deleteCheck();} return false;"">&nbsp; ")
        End IF
        Response.Write("<input type='submit' value='Save changes'>")
        Response.Write("</td>")
        Response.Write("</tr>")
        Response.Write("</form>")
        Response.Write("<tr><td colspan='3'><hr class='hrHeadline'></td></tr>")
        Response.Write("</table>")
        Response.Write("</td>")
        Response.Write("<td width='100'></td>")
        Response.Write("<td valign='top'>")
        Response.Write("<table cellpadding='2' cellspacing='2' width='450'>")
        Response.Write("<tr><td class='headline' colspan='2'>LEASE TAKER STATISTICS<hr class='hrHeadline'></td></tr>")
        Response.Write("<tr bgcolor='#eeeeee'><td>Number of drivers:</td><td align='right'>" & driverCount & "</td></tr>")
        Response.Write("<tr><td>Number of lease contracts:</td><td align='right'>" & vehicleCount & "</td></tr>")
        Response.Write("<tr bgcolor='#eeeeee'><td>Number of claims reported:</td><td align='right'>" & claimCount & "</td></tr>")
        Response.Write("<tr><td>Total claim cost (" & countryCurrency & "):</td><td align='right'>" & FormatNumber(CDbl(claimSum), 2) & "</td></tr>")
        Response.Write("<tr><td colspan='2'><hr class='hrHeadline'></td></tr>")
        Response.Write("<tr><td colspan='2'><br></td></tr>")
        If Len(vehicleID) > 0 Then 
            Response.Write("<tr><td class='headline' colspan='2'>INFORMATION<hr class='hrHeadline'></td></tr>")
            Response.Write("<tr bgcolor='#eeeeee'><td colspan='2'><font color='red'>OBS!</font> The change of the lease takers information will also accur to the " & vehicleCount & " lease contracts, witch are connected to the lease taker.<br><br>If you only want to change the lease takers information on this specific lease contract, please <a href='changeClient.aspx?vehicleID=" & vehicleID & "'>click here</a>.</td></tr>")
            Response.Write("<tr><td colspan='2'><hr class='hrHeadline'></td></tr>")
        End IF
        If CDbl(vehicleCount) > 0 Or CDbl(driverCount) > 0 Then 
            Response.Write("<tr><td class='headline' colspan='2'>INFORMATION<hr class='hrHeadline'></td></tr>")
            Response.Write("<tr bgcolor='#eeeeee'><td colspan='2'><font color='red'>OBS!</font> You can not delete this lease taker, because there are drivers and/or lease contracts connected to it.</td></tr>")
            Response.Write("<tr><td colspan='2'><hr class='hrHeadline'></td></tr>")
        End IF
        Response.Write("</table>")
        Response.Write("</td>")
        Response.Write("</tr>")
        Response.Write("</table>")
        Under()
    ElseIf Request.QueryString("do") = "save" Then 
intClientID = Request.QueryString("clientID")
        clientType = Request.Form("clientType")
        client = Request.Form("client")
        cin = Request.Form("cin")
        street = Request.Form("street")
        zipcode = Request.Form("zipcode")
        city = Request.Form("city")
        areacode = Request.Form("areaCode")
        telephone = Request.Form("telephone")
        email = Request.Form("email")
        userID = fragusLeasingUserID
intClientID = CLng(intClientID)
        clientType = CLng(clientType)
        If Len(client) > 0 Then 
            client = Trim(client)
            client = Replace(CStr(client), "'", "")
            client = Replace(CStr(client), """", "")
            client = Replace(CStr(client), "<", "")
            client = Replace(CStr(client), ">", "")
            client = Replace(CStr(client), "=", "")
        End IF
        If Len(cin) > 0 Then 
            cin = Trim(cin)
            cin = Replace(CStr(cin), "'", "")
            cin = Replace(CStr(cin), """", "")
            cin = Replace(CStr(cin), "<", "")
            cin = Replace(CStr(cin), ">", "")
            cin = Replace(CStr(cin), "=", "")
        End IF
        If Len(street) > 0 Then 
            street = Trim(street)
            street = Replace(CStr(street), "'", "")
            street = Replace(CStr(street), """", "")
            street = Replace(CStr(street), "<", "")
            street = Replace(CStr(street), ">", "")
            street = Replace(CStr(street), "=", "")
        End IF
        If Len(zipcode) > 0 Then 
            zipcode = Trim(zipcode)
            zipcode = Replace(CStr(zipcode), "'", "")
            zipcode = Replace(CStr(zipcode), """", "")
            zipcode = Replace(CStr(zipcode), "<", "")
            zipcode = Replace(CStr(zipcode), ">", "")
            zipcode = Replace(CStr(zipcode), "=", "")
        End IF
        If Len(city) > 0 Then 
            city = Trim(city)
            city = Replace(CStr(city), "'", "")
            city = Replace(CStr(city), """", "")
            city = Replace(CStr(city), "<", "")
            city = Replace(CStr(city), ">", "")
            city = Replace(CStr(city), "=", "")
        End IF
        If Len(areacode) > 0 Then 
            areacode = Trim(areacode)
            areacode = Replace(CStr(areacode), "'", "")
            areacode = Replace(CStr(areacode), """", "")
            areacode = Replace(CStr(areacode), "<", "")
            areacode = Replace(CStr(areacode), ">", "")
            areacode = Replace(CStr(areacode), "=", "")
        End IF
        If Len(telephone) > 0 Then 
            telephone = Trim(telephone)
            telephone = Replace(CStr(telephone), "'", "")
            telephone = Replace(CStr(telephone), """", "")
            telephone = Replace(CStr(telephone), "<", "")
            telephone = Replace(CStr(telephone), ">", "")
            telephone = Replace(CStr(telephone), "=", "")
        End IF
        If Len(email) > 0 Then 
            email = Trim(email)
            email = Replace(CStr(email), "'", "")
            email = Replace(CStr(email), """", "")
            email = Replace(CStr(email), "<", "")
            email = Replace(CStr(email), ">", "")
            email = Replace(CStr(email), "=", "")
        End IF
        dbOpen()
Conn.Execute("Update tblClient Set userID = " & userID & ", clientType = " & clientType & ", client = '" & client & "', cin = '" & cin & "', street = '" & street & "', zipcode = '" & zipcode & "', city = '" & city & "', areacode = '" & areacode & "', telephone = '" & telephone & "', email = '" & email & "' Where clientID = " & intClientID & "")
        dbClose()
Response.Redirect("editClient.aspx?subDo=%20-%20SAVED&clientID=" & intClientID & "")
    ElseIf Request.QueryString("do") = "delete" Then 
intClientID = Request.QueryString("clientID")
intClientID = CLng(intClientID)
        dbOpen()
Conn.Execute("Delete From tblClient Where clientID = " & intClientID & "")
        dbClose()
        Response.Redirect("addClient.aspx?subDo=%20-%20DELETED")
    End IF
%>
